<template>
  <div class="number-allocation">
    <div class="header">
      <el-button type="info" style="background-color: #6e767c" @click="router.push('/nav')">回主页</el-button>
    </div>
    <h2>我的靓号库</h2>
    <div style="font-size: 18px; color: #000">总数：{{tableData.length}}</div>
    <div style="padding: 0 12px; margin-top: 12px">
      <el-table
          class="my-table"
          :data="tableData"
          style="width: 100%"
      >
        <el-table-column label="生肖" property="billType" width="60">
          <template #default="scope">{{ scope.row.billType === 0 ? '龙钞' : '蛇钞' }}</template>
        </el-table-column>
        <el-table-column property="serialNumber" label="冠字号"/>
        <el-table-column property="price" label="价格" width="60"/>
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button v-if="scope.row.status == 1" style="width: 72px; height: 28px !important;" type="primary" @click="()=>onOff(scope.row)">下架</el-button>
            <el-button v-else style="width: 72px; height: 28px !important;" type="primary" @click="record=scope.row; dialogShow=true">上架</el-button>
          </template>
        </el-table-column>
      </el-table>
      <van-dialog v-model:show="dialogShow" title="上架" show-cancel-button @confirm="onConfirm" @cancel="record={}; myPrice = 0">
        <div class="flex" style="padding: 24px">
          <span>上架价格：</span>
          <el-input-number v-model="myPrice" :min="0" :precision="0"/>
        </div>
      </van-dialog>
    </div>
  </div>
</template>

<script setup>
import router from "@/router";
import {ref} from "vue";
import {getMyNiceCodes, updateMyNiceCord} from "@/api/api";
import {ElMessage} from "element-plus";
import {showConfirmDialog} from "vant";
const tableData = ref([])
const dialogShow = ref(false)
const record = ref({})
const myPrice = ref(0)

const getMyList = async () => {
  const res = await getMyNiceCodes()
  if (res.code !== 200) {
    return ElMessage.error({
      message: res.msg,
      type: 'error',
    });
  }
  tableData.value = res.rows
}

const onOff = (row) => {
  row.status = 0
  showConfirmDialog({
    title: '',
    message: '确定要下架吗',
  })
      .then(() => {
        updateMyNiceCord(row).then(res =>{
          if (res.code === 200){
            ElMessage.success('下架成功')
            getMyList()
          }else {
            ElMessage.error(res.msg)
          }
        })
      })
      .catch(() => {

      });
}

const onConfirm = () => {
  record.value.status = 1
  record.value.price = myPrice
  updateMyNiceCord(record.value).then(res =>{
    if (res.code === 200){
      ElMessage.success('上架成功')
      record.value = {}
      myPrice.value = 0
      getMyList()
    }else {
      ElMessage.error(res.msg)
    }
  })
}

getMyList()
</script>

<style lang="less">
.number-allocation {
  height: 100%;

  .header {
    display: flex;
    padding: 12px;
    box-shadow: 0 10px 15px -5px rgba(0,0,0,0.12);
  }

  .detail_box {
    & > div {
      display: flex;
      font-size: 18px;
      color: #000;
      margin-bottom: 12px;

      .label {
        font-weight: bold;
      }
    }
  }

  .my-table {
    td .el-button > span {
      font-size: 13px !important;
    }

    .el-button--primary {
      background-color: #1677ff !important;
    }
  }

  .el-table th.el-table__cell {
    background-color: #1677ff !important;
    color: #ffffff !important;
  }

  .el-table__header-wrapper {
    border-radius: 8px 8px 0 0;
  }
}
</style>
